﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Entity;
using Job.Logic.Company;
using Job.Web;

namespace Job.Web.userControls.Company
{
    public partial class CompanyInfo : BaseCtrl
    {
        private int CompanyId
        {
            get
            {
                //后台管理员能通过后台为企业增加职位，所以在此处需要兼职企业ID从url或从Session中获取
                int id = myR["company"].IntValue;
                if (id > 0) return id;
                MySession session = new MySession();
                SessionCompany sCompany = session.GetCompanyFromSession();
                if (null == sCompany)
                    return 0;
                else
                    return sCompany.CompanyId;
            }
        }

        CompanyLogic logic = new CompanyLogic();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.message.Visible = false;
            if ( 0 == CompanyId ) 
            {
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=1");
                return;
            }
            if ( !this.IsPostBack ) LoadBaseInfo();            
        }


        private bool LoadBaseInfo()
        {
            JobCompanyInfo info = logic.GetCompanyInfo(CompanyId);
            if ( null == info )
            {
                this.message.InnerText = "系统内部异常，查询不到企业资料！";
                this.message.Visible = true;
                Response.Redirect("CompanyLogin.aspx?categoryid=2&error=2");
                return false;
            }

            tbName.Text = info.CompanyName;

            string property = string.Empty;
            if (info.CompanyProperty.HasValue) property = info.CompanyProperty.ToString();
            WebCommon.SetDropDownFromDict(ddlProperty,property,
                Constants.GetDictionaryCode(Constants.DictionaryType.COMPANY_PROPERTY),true);

            WebCommon.LoadLevelDropDownList(ddlIndustryFather,ddlIndustrySub,IndustryRelate,
                Constants.GetParentCodeBySubCode(info.CompanyIndustry),
                info.CompanyIndustry, Constants.DictionaryType.INDUSTRY_CLASS);

            tbFunds.Text = info.RegistFunds;

            string scope = string.Empty;
            if (info.CompanyProperty.HasValue) scope = info.CompanyScope.ToString();
            WebCommon.SetDropDownFromDict(ddlScope,scope,
                Constants.GetDictionaryCode(Constants.DictionaryType.COMPANY_SCOPE),true);

            WebCommon.LoadLevelDropDownList(ddlAddrFather,ddlAddrSub,AddressRate,
                Constants.GetParentCodeBySubCode(info.CompanyAddr),
                info.CompanyAddr, Constants.DictionaryType.ADDR_CITY);

            tbTitle.Text = info.CompanyTitles;
         
            string secret = "0";
            if ( info.SecretSet.HasValue) secret = info.SecretSet.ToString();
            this.rblSecret.Text = secret;
            
            tbPhone.Text = info.ContactPhone;
            tbMobile.Text = info.ContactMobile;
            tbFax.Text = info.ContactFax;
            tbLinkman.Text = info.ContactMan;
            tbMail.Text = info.ContactEmail;
            tbHomePage.Text = info.HomePage;
            tbPostCode.Text = info.ContactPostcode;
            tbAddr.Text = info.ContactAddr;

            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            JobCompanyInfo info = new JobCompanyInfo();
            info.CompanyInfoId = CompanyId;
            info.CompanyProperty = Convert.ToInt32(ddlProperty.SelectedValue);
            info.CompanyIndustry = ddlIndustrySub.SelectedValue;
            info.RegistFunds = tbFunds.Text;
            info.CompanyScope = Convert.ToInt32(ddlScope.SelectedValue);
            info.CompanyAddr = ddlAddrSub.SelectedValue;
            info.CompanyTitles = tbTitle.Text;
            info.SecretSet = Convert.ToInt32(rblSecret.SelectedValue);
            info.ContactAddr = tbAddr.Text;
            info.ContactEmail = tbMail.Text;
            info.ContactFax = tbFax.Text;
            info.ContactMan = tbLinkman.Text;
            info.ContactPhone = tbPhone.Text;
            info.ContactMobile = tbMobile.Text;
            info.ContactPostcode = tbPostCode.Text;
            info.HomePage = tbHomePage.Text;

            try
            {
                logic.UpdateBaseInfo(info);
                this.message.InnerHtml = "<font color='#990000'>保存企业资料成功！</font>";                
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex.StackTrace);
                this.message.InnerHtml = "<font color='#ff0000'>系统内部异常，保存企业资料失败：" + ex.Message + "</font>";               
            }
            this.message.Visible = true;
            this.message.Height = "30px";
        }

    }
}